package com.lanou3g.musicenjoy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;

import com.lanou3g.musicenjoy.entity.Menu;
import com.lanou3g.musicenjoy.adapter.MenuAdapter;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class MenuFragment extends Fragment implements AdapterView.OnItemClickListener {
    private List<Menu.ContentEntity> datas;
    private MenuAdapter adapter;
    private RequestQueue queue;
    private GridView gridView;
    private View view;
    private MenuItemFragment fragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_new,null);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        queue= VolleySingleton.getInstance(getActivity()).getRequestQueue();
        adapter=new MenuAdapter(getActivity());
        gridView= (GridView) view.findViewById(R.id.newGrideView);
        gridView.setOnItemClickListener(this);
        gridView.setAdapter(adapter);

        String url="http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&" +
                "method=baidu.ting.diy.gedan&page_size=30&page_no=1";
        StringRequest request=new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response!=null){
                    Gson gson=new Gson();
                    Menu result=gson.fromJson(response,Menu.class);
                    if (result!=null){
                        datas=result.getContent();
                        adapter.initData(datas);
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        fragment=new MenuItemFragment();
        Bundle bundle=new Bundle();
        String b=datas.get(position).getListid();
        Log.i("FAKJFKA",b);
        String url=datas.get(position).getPic_w300();
       // String time=datas.get(position).getTitle();
        bundle.putString("id",b);
        bundle.putString("url",url);
       // bundle.putString("time", time);
        fragment.setArguments(bundle);
        getFragmentManager()
                .beginTransaction()
                .addToBackStack(null)
                .replace(R.id.container,fragment)
                .commit();
    }
}
